package com.mybatils.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/5/21 14:21
 * @since JDK 1.8
 */
public class TextUtils {

    /**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String getTextString(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String string = null;
            while((string = br.readLine())!=null){//使用readLine方法，一次读一行
                result.append(System.lineSeparator()).append(string);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }

    public static List<String> getTextLineList(String fileName){
        String path=System.getProperty("user.dir");
        File file=new File(path+File.separator+fileName);
        return getTextLineList(file);
    }

    public static void main(String[] args) {
        List<String> stringList=new ArrayList<>();
        for (int i = 0; i < 100 ; i++) {
            stringList.add("helloword");
        }
        wirteTextLine(stringList,"name");
    }


    public static boolean wirteTextLine(List<String> stringList,String fileName){
        String path=System.getProperty("user.dir");
        File file=new File(path+File.separator+fileName);
        return writeTxtList(stringList,file);
    }

    /**
     * 获取文本，一行为一个元素
     * @param file
     * @return
     */
    public static List<String> getTextLineList(File file){
        List<String> stringList=new ArrayList<>();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String string = null;
            while((string = br.readLine())!=null){//使用readLine方法，一次读一行
                stringList.add(string);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return stringList;
    }

    public static boolean writeTxtList(List<String> strings,File file){
        boolean flag=false;
        StringBuffer stringBuffer=new StringBuffer();
        strings.forEach(e->{
            stringBuffer.append(e).append(System.lineSeparator());
        });
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            StringBuffer buf = new StringBuffer();
            buf.append(stringBuffer.toString());
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            flag=true;
        }catch (IOException e1){
            e1.printStackTrace();
        }finally {
            if (pw != null) {
                pw.close();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;

    }


    public static boolean writeTxtFile2(File file,String newStr) throws IOException {
        // 先读取原有文件内容，然后进行写入操作
        boolean flag = false;
        String filein = newStr + "\r\n";
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            StringBuffer buf = new StringBuffer();
            buf.append(filein);
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            flag = true;
        } catch (IOException e1) {
            // TODO 自动生成 catch 块
            throw e1;
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }

        }
        return flag;
    }



}
